/*
 *   This file is part of Verbum.
 *
 *   poat is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   poat is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with poat.  If not, see <http://www.gnu.org/licenses/>.
 * 
 *   Copyright (c) 2008 Sergio Gabriel Teves <gabriel.sgt at gmail.com>
 */ 

Verbum.Utilities = function(config){
    Ext.apply(this, config);
    Verbum.Utilities.superclass.constructor.call(this);
};

Ext.extend(Verbum.Utilities, Ext.util.Observable, {
	confirm: function(text, fnYES, fnNO, scope) {
	    Ext.Msg.confirm(_('Confirm'),
	     text, function(btn) {
	         	if (btn=="yes") {
					if (fnYES!=undefined) {
						fnYES.call(scope || null);
					}	
	 			} else {
					if (fnNO!=undefined) {
						fnNO.call(scope || null);
					} 
	 			}
			} 
		);	
	},
	ask: function(options) {
		var parent = this;
		this.addEvents(
			'press_ok',
			'press_cancel'
		);
		
		if (options.allowBlank!==false) {
			options.allowBlank = true;
		}
		
	    var form = new Ext.form.FormPanel({
	    	labelAlign: 'top',
	        baseCls: 'x-plain',
	        defaultType: 'textfield',
	        buttonAlign:'right',
	        autoWidth: true,
	        autoHeight: true,
	        layout:'form',
	        defaults: {msgTarget:'side', allowBlank: options.allowBlank, anchor: '95%'},
	        items: [{
	            fieldLabel: options.msg,
	            name: 'text',
	            id: 'text',
	            maxLength: options.maxLength || 255
	        }],
	        buttons: [{
	            text: Ext.Msg.buttonText.ok,
	            handler: function() {
	            	if (form.getForm().isValid()) {
		            	parent.fireEvent('press_ok',form.findById('text').getValue());
		            	win.close();
		            }
	            } 
	        },{
	            text: Ext.Msg.buttonText.cancel,
	            handler: function() {
	            	parent.fireEvent('press_cancel');
	            	win.close();
	            }
	        }]
	    });

	    var win = new Ext.Window({
	    	manager: MyDesktop.getDesktop().getManager(),
	        title: options.title,
	        width: 400,
	        minWidth: 400,
	        minHeight: 250,
	        //layout: 'fit',
	        bodyStyle:'padding:5px;',
	        items: form,
			resizable: false,
			closable: false,
			draggable: true,
			modal: true,
			iconCls: 'comment-icon'
	    });
	
	    win.show();
	    
	    win.on({
	    	'show': function() {
	    		focus(form,'text');
	    	}
	    });
	    
	},
	request: function(options) {
        
        var parent = this;
        
        this.addEvents(
            'done',
            'fail',
            'hide'
        );
        
        var lMask;
		if (options.el) {
			lMask = new Ext.LoadMask(options.el, {msg: options.msg || _('Please wait...'), removeMask: true});
	    	lMask.show();
		}
		
		Ext.Ajax.request({
			waitMsg: _('Please wait...'),
            url: getMainUrl(), 
            params: options.params,
            success: function(response){
            	obj = Ext.util.JSON.decode(response.responseText);
            	if (obj.success==false) {
            		if (options.warn!=false) Ext.Msg.warn(obj.error);
            		parent.fireEvent('fail',response);	
            	} else {
            		parent.fireEvent('done',response);	
            	}
            },
			callback: function ( options, success, response ) { 
				if (lMask!=undefined) lMask.hide();
				parent.fireEvent('hide');
			}
		});
		
		return this;
	},
	
	messageForm: function(options) {
		  var mForm = new Ext.form.FormPanel({
		      baseCls: 'x-plain',
		      url:getMainUrl(),
		      baseParams: {action: 'message', op: 'user'},
		      defaultType: 'textfield',
		      buttonAlign:'right',
		      waitMsgTarget: true,
		      bbar: new Ext.Toolbar({
						items:['->',
							{
								xtype:'button',
								tooltip: _('Cancel'),
								iconCls: 'cancel',
								text: _('Cancel'),
								handler: function() {
									mWin.close();
								}
							},
							{
								xtype:'button',
								tooltip: _('Send'),
								iconCls: 'send',
								text: _('Send'),
								handler: function() {
									if (mForm.getForm().isValid()) {
						                    mForm.getForm().submit({ 
						                        method:'POST', 
						                        waitMsg: _('Sending...'),
						 						
						                        success:function(form, action){
						                        	obj = Ext.util.JSON.decode(action.response.responseText);
						                        	if (options.onSuccess!==false) {
						                        		Ext.Msg.message(obj.ret);
						                        	}
						                        	mWin.close();
						                        },
						 
						                        failure:function(form, action){ 
					                                obj = Ext.util.JSON.decode(action.response.responseText); 
					                                Ext.Msg.error(obj.error); 
						                        } 
						                    });								
									}
								}
							}
						 ]		
					}),
		      autoWidth: true,
		      autoHeight: true,
		      defaults: {msgTarget:'side', allowBlank: false, anchor: '95%'},
		      monitorResize: true,
		      items: [{
		          fieldLabel: _('Subject'),
		          name: 'subject',
		          id: 'subject',
		          selectOnFocus: true,
		      },{
		          hideLabel: true,
		          name: 'text',
		          xtype: 'textarea',
		          selectOnFocus: true,
		          height: 120
		      },{
		          name: 'ids',
		          xtype: 'hidden',
		          value: options.ids
		      }]
		  });
		
		  			
	    var mWin = MyDesktop.getDesktop().createWindow({
			//manager: MyDesktop.getDesktop().getManager(),
		    title: _('Send PM'),
		    width: 450,
		    minWidth: 450,
		    minHeight: 250,
		    layout: 'fit',
		    bodyStyle:'padding:5px;',
		    items: mForm,
			resizable: false,
			closable: true,
			draggable: true,
			iconCls: 'mail',
            listeners: {
                'show': {
                    fn: function(){
                    	focus(mForm,'subject');
                	}
                }
            }
		  });
		
		  mWin.show();
		  			
	}
});
